package com.edushare.common.dao;

import java.util.Collection;
import java.util.Iterator;

import com.edushare.common.orm.ORMInterface;
import com.edushare.common.vo.FileDirectories;
import com.edushare.common.vo.FileFolderObject;
import com.edushare.common.vo.FilesUploaded;

public class FileFolderObjectDAO extends CayenneBasedDAO {

	public FileFolderObjectDAO(ORMInterface ormInterface) {
		super(ormInterface);
	}

	public void deleteFileOrFolder(Collection<FileFolderObject> fileFolderObjectCollection) {
		Iterator<FileFolderObject> it = fileFolderObjectCollection.iterator();
		while(it.hasNext()){
			FileFolderObject ffo = it.next();
			it.remove();
			deleteFileFolderObject(ffo);
		}
	}

	public void deleteFileFolderObject(FileFolderObject ffo) {
		if(ffo.getType() == FileFolderObject.FILE_TYPE){
			FilesUploaded fu = new FilesUploadedDAO(ormInterface).getFileById(ffo.getId());
			new FilesUploadedDAO(ormInterface).deleteFileUploaded(fu);
		}else if(ffo.getType() == FileFolderObject.FOLDER_TYPE){
			FileDirectories fd = new FileDirectoriesDAO(ormInterface).getFileDirById(ffo.getId());
			new FileDirectoriesDAO(ormInterface).deleteDirectoryAndFiles(fd);
		}
	}

}
